package isc.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class IscLog
{
  private String logFileName = "isc.log";
  private File logFile;
  private File tmpFile;
  private PrintWriter log;
  private int logLevel = 0;

  public IscLog()
  {
    try
    {
      this.logFile = new File(this.logFileName);
      if (this.logFile.exists())
      {
        Calendar clr = Calendar.getInstance();

        String todayFileName = "isc" + clr.get(5) + ".log";

        this.tmpFile = new File(todayFileName);

        int todayMonth = clr.get(2);
        clr.setTimeInMillis(this.tmpFile.lastModified());
        int tmpFileMonth = clr.get(2);
        clr.clear();

        if (tmpFileMonth != todayMonth)
        {
          this.log = new PrintWriter(new FileWriter(todayFileName, false), true);
        }
        else
        {
          this.log = new PrintWriter(new FileWriter(todayFileName, true), true);
        }

      }

    }
    catch (IOException e)
    {
      throw new RuntimeException(e.toString());
    }
  }

  public IscLog(String logFileName)
  {
    int index = logFileName.indexOf(".");
    String fileExt = ".log";

    if (index != -1)
    {
      fileExt = logFileName.substring(index);
      logFileName = logFileName.substring(0, index);
    }

    try
    {
      this.logFile = new File(logFileName + fileExt);

      if (this.logFile.exists())
      {
        Calendar clr = Calendar.getInstance();

        String todayFileName = logFileName + clr.get(5) + fileExt;

        this.tmpFile = new File(todayFileName);

        int todayMonth = clr.get(2);
        clr.setTimeInMillis(this.tmpFile.lastModified());
        int tmpFileMonth = clr.get(2);
        clr.clear();

        if (tmpFileMonth != todayMonth)
        {
          this.log = new PrintWriter(new FileWriter(todayFileName, false), true);
        }
        else
        {
          this.log = new PrintWriter(new FileWriter(todayFileName, true), true);
        }

      }

    }
    catch (IOException e)
    {
      throw new RuntimeException(e.toString());
    }
  }

  private String getDate(String pattern)
  {
    SimpleDateFormat df = new SimpleDateFormat(pattern);
    return df.format(new Date());
  }

  public void printf(int debugLevel, String msg)
  {
    if (this.logLevel == 9)
    {
      System.out.println(getDate("yyyyMMdd HH:mm:ss") + ": " + msg);
    }
    else if ((this.log != null) && 
      (debugLevel <= this.logLevel))
    {
      this.log.println(getDate("yyyyMMdd HH:mm:ss") + ": " + msg);
    }
  }

  public void printf(String msg)
  {
    if (this.log != null)
    {
      this.log.println(getDate("yyyyMMdd HH:mm:ss") + ": " + msg);
    }
  }

  public void setLogLevel(int level)
  {
    this.logLevel = level;
  }

  public void printf(int msg)
  {
    if (this.log != null)
    {
      this.log.println(getDate("yyyyMMdd HH:mm:ss") + ": " + msg);
    }
  }

  public void printf(Throwable e, String msg)
  {
    if (this.log != null)
    {
      this.log.println(getDate("yyyyMMdd HH:mm:ss") + ": " + msg);
      e.printStackTrace(this.log);
    }
  }

  public static void main(String[] arg)
  {
    System.out.println("ddddddddddddddddddddd");
  }
}